package com.mlethe.library.socialtool.core;

import android.os.Handler;
import android.os.Looper;

import com.mlethe.library.socialtool.callback.Callback;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HttpsURLConnection;

/**
 * 网络请求
 * @author Mlethe
 */
class HttpsThread extends Thread {

    private Callback mCallback;
    private String mUrl;
    private Handler mHandler;

    public HttpsThread(String url, Callback callback) {
        this.mUrl = url;
        this.mCallback = callback;
        this.mHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    public void run() {
        InputStream in;
        try {
            URL url = new URL(mUrl);
            URLConnection urlConnection = url.openConnection();
            HttpsURLConnection httpsConn = (HttpsURLConnection) urlConnection;
            httpsConn.setAllowUserInteraction(false);
            httpsConn.setInstanceFollowRedirects(true);
            httpsConn.setRequestMethod("GET");
            httpsConn.connect();
            final int resCode = httpsConn.getResponseCode();
            final String resMsg = httpsConn.getResponseMessage();
            if (resCode == HttpURLConnection.HTTP_OK) {
                in = httpsConn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                in.close();
                String httpResult = sb.toString();
                final JSONObject jsonObject = new JSONObject(httpResult);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            try {
                                mCallback.onSuccess(jsonObject);
                            } catch (Exception e) {
                                if (mCallback != null) {
                                    mCallback.onError(e);
                                }
                            }
                        }
                    }
                });
            } else {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onError(new HttpException(resCode, resMsg));
                        }
                    }
                });
            }
            httpsConn.disconnect();
        } catch (final Exception e) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (mCallback != null) {
                        mCallback.onError(e);
                    }
                }
            });
        } finally {
            mHandler = null;
        }
    }
}
